/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.actions;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import edu.pku.sei.modeler.model.structure.MetaDiagram;

/**
 * @author danny
 *
 */
public class OpenMultiDiagramDialog {
	public static final String TITLE = "Open Element";
	public static final int HEIGHT = 300;
	public static final int WIDTH = 500;
	
	private Shell shell;
	private Table table;
	private int returnCode = SWT.CANCEL;
	private List<MetaDiagram> diagrams;
	private List<MetaDiagram> result;
	
	public OpenMultiDiagramDialog(Shell parent) {
		shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.SYSTEM_MODAL);
		shell.setSize(WIDTH, HEIGHT);
		shell.setText(TITLE);
		result = new ArrayList<MetaDiagram>();
	}

	public int open() {
		createControl(shell);
		shell.open();
		while (!shell.isDisposed())
			while (!shell.getDisplay().readAndDispatch())
				shell.getDisplay().sleep();
		return returnCode;
	}
	
	public void createControl(Shell shell) {
		GridLayout gridLayout = new GridLayout();
		gridLayout.marginHeight = 10;
		gridLayout.marginWidth = 10;
		shell.setLayout(gridLayout);
		
		Label label = new Label(shell, SWT.NONE);
		label.setText("This element is shown on those diagrams, choose the one(s) you want to open:");
		
		createTable(shell);
		createButtonComp(shell);
	}
	private void createTable(Shell shell) {
		table = new Table(shell, SWT.CHECK | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		GridData data = new GridData(GridData.FILL_BOTH 
				| GridData.GRAB_HORIZONTAL
				| GridData.GRAB_VERTICAL);
		table.setLayoutData(data);
		TableColumn tc1 = new TableColumn(table, SWT.NONE);
		tc1.setText("Name");
		tc1.setWidth(200);
		
		TableColumn tc2 = new TableColumn(table, SWT.NONE);
		tc2.setText("Location");
		tc2.setWidth(200);
		
		if(diagrams != null) {
			for(MetaDiagram diag:diagrams) {
				TableItem ti = new TableItem(table, SWT.NONE);
				ti.setText(0, diag.getName());
				ti.setText(1, diag.getNamespace());
			}
		}
	}
	private void createButtonComp(final Shell shell) {
		Composite btnComp = new Composite(shell, SWT.NULL);
		GridData btnData = new GridData(GridData.GRAB_HORIZONTAL
				| GridData.FILL_HORIZONTAL);
		btnComp.setLayoutData(btnData);
		GridLayout btnLayout = new GridLayout();
		btnLayout.numColumns = 4;
		btnComp.setLayout(btnLayout);

		Button selectAll = new Button(btnComp, SWT.PUSH);
		selectAll.setText("Select All");
		selectAll.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				TableItem [] items = table.getItems();
				for(int i=0; i<items.length; i++) {
					items[i].setChecked(true);
				}
			}
		});
		
		Button selectNone = new Button(btnComp, SWT.PUSH);
		selectNone.setText("Deselect All");
		selectNone.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				TableItem [] items = table.getItems();
				for(int i=0; i<items.length; i++) {
					items[i].setChecked(false);
				}
			}
		});
		
		Button ok = new Button(btnComp, SWT.PUSH);
		ok.setText("     OK     ");
		ok.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
				| GridData.HORIZONTAL_ALIGN_END));
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				returnCode = SWT.OK;
				generateResult();
				shell.dispose();
			}
		});

		Button cancel = new Button(btnComp, SWT.PUSH);
		cancel.setText("   Cancel   ");
		cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
	}
	
	private void generateResult() {
		result.clear();
		TableItem [] items = table.getItems();
		for(int i=0; i<items.length; i++) {
			if(items[i].getChecked()) {
				result.add(diagrams.get(i));
			}
		}
	}
	
	/**
	 * @return the result
	 */
	public List<MetaDiagram> getResult() {
		return result;
	}

	/**
	 * @param diagrams the diagrams to set
	 */
	public void setDiagrams(List<MetaDiagram> diagrams) {
		this.diagrams = diagrams;
	}
}
